package com.example.androiddemo01.views;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.androiddemo01.R;
import com.example.androiddemo01.bean.Course;
import com.example.androiddemo01.database.DBHelper;
import com.example.androiddemo01.widget.HeaderView;

public class AddCourseActivity extends AppCompatActivity implements View.OnClickListener {

   private HeaderView mHeaderView;
   private EditText mEtName;
   private EditText mEtCode;
   private EditText mEtTeacher;
   private EditText mEtHours;

   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_add_course);
      mHeaderView = findViewById(R.id.title_bar);
      mHeaderView.setTitle("添加课程");
      mEtName = findViewById(R.id.et_course_name);
      mEtCode = findViewById(R.id.et_course_code);
      mEtTeacher = findViewById(R.id.et_course_teacher);
      mEtHours = findViewById(R.id.et_learning_hours);
      mHeaderView.setRightClickListener(this);
   }

   @Override
   public void onClick(View v) {
      String name = mEtName.getText().toString();
      String code = mEtCode.getText().toString();
      String teacher = mEtTeacher.getText().toString();
      String hours = mEtHours.getText().toString();

      if (allInputNotEmpty(name, code, teacher, hours)) {
         Course course = new Course();
         course.setCourse_code(code);
         course.setCourse_name(name);
         course.setCourse_teacher(teacher);
         course.setLearning_hours(hours);
         int id = (int) DBHelper.getInstance(this).insertCourse2(course);
         if (id >= 0) {
            Toast.makeText(this, "添加成功,可继续添加", Toast.LENGTH_SHORT).show();
            mEtName.setText("");
            mEtCode.setText("");
            mEtTeacher.setText("");
            mEtHours.setText("");
         }
      }
   }

   private boolean allInputNotEmpty(String... inputStrings) {
      for (int i = 0; i < inputStrings.length; i++) {
         if (TextUtils.isEmpty(inputStrings[i])) {
            return false;
         }
      }
      return true;
   }
}
